package com.aurora.source.zxing;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;

import com.aurora.source.R;
import com.aurora.source.base.BasePresenter;
import com.aurora.source.base.BaseToolbarActivity;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;

/**
 * <pre>
 * author: 王伟
 * time:2017/06/17
 * desc:二维码扫描界面
 * <pre>
 */
public class CaptureActivity extends BaseToolbarActivity {

    @BindView(R.id.fl_zxing_container)
    FrameLayout flMyContainer;
    private CaptureFragment captureFragment;

    @Override
    public BasePresenter attach() {
        return null;
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("scan_result", result);
            //传bitmap会卡住
            // bundle.putParcelable("bitmap", mBitmap);
            resultIntent.putExtras(bundle);
            CaptureActivity.this.setResult(RESULT_OK, resultIntent);
            CaptureActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            CaptureActivity.this.setResult(0, resultIntent);
            CaptureActivity.this.finish();
        }
    };

    @Override
    public int bindLayout() {
        return R.layout.activity_capture;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        simpleToolbar.setMainTitle("二维码扫描");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        // 为二维码扫描界面设置定制化界面  com.uuzuche.lib_zxing.
        captureFragment = new CaptureFragment();
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_zxing_container, captureFragment).commit();

    }

    @Override
    public void doBusiness() {

    }
}
